package com.roc.web.action.windcontroll;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.BlackListBean;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.service.black.IBlackService;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;

@SuppressWarnings("serial")
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/blackAudit")
public class BlackAuditAction   extends PaginationBaseAction {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger=LogManager.getLogger(BlackAuditAction.class);
	private BlackAuditSearchCondition searchCondition;
	private BlackListBean black;
	
	private String blackNum;
	@Autowired
	IPageService pagenitionService;
	@Autowired
	IBlackService blackService;
	
	
	
	/**
	 * 初始化黑名单页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/windControllSta/blackAudit.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })	
	public String init() throws Exception{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode()) || op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())|| op.getRoleType().equals(Role.SYSTEM.getCode()))
		{
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;	   
    }
	
	
	@Action(value = "blackAuditlistview", results = { @Result(name = "success", location = "/pages/windControllSta/blackAudit.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String blackView() throws Exception{
		logger.info("审核查询黑名单");
		try {
			if(searchCondition==null){
				searchCondition=(BlackAuditSearchCondition)getSearchConditionFromSession();
				if(searchCondition==null){
					searchCondition=new BlackAuditSearchCondition();
				}
			}
			
			int rows=pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			}
			List<BlackListBean> blackList = new ArrayList<BlackListBean>();
			queryBlackList(blackList,searchRsList);
			
			getRequest().setAttribute("blackList", blackList);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			
			setSearchToConditionSession(searchCondition);
			addActionMessage("当前条件下统计到【"+rows+"】条记录");

			
		} catch (Exception e) {
			
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}		
	   return SUCCESS;
    }
	
	/**
	 * 查询
	 */
	 public  void queryBlackList(List<BlackListBean> blackList,List<Object> searchRsList){
		    
			if(searchRsList!=null&&searchRsList.size()>0){
				Iterator iterator=searchRsList.iterator();
				while (iterator.hasNext()) {
					Object[] obj=(Object[]) iterator.next();
					BlackListBean bean=new BlackListBean();
					bean.setBlackNum(Constance.Null2EmptyTrim(obj[0]));
					bean.setBlackType(Constance.Null2EmptyTrim(obj[1]));
					bean.setDate(Constance.Null2EmptyTrim(obj[2]));
					bean.setTime(Constance.Null2EmptyTrim(obj[3]));
					bean.setReason(Constance.Null2EmptyTrim(obj[4]));
					bean.setAddopName(Constance.Null2EmptyTrim(obj[5]));
					blackList.add(bean);
			}
			
			
		}
	 }
	
	      /**
	       * 黑名单审核通过
	       * @return
	       */
		@Action(value = "blackAuditPass", results = { @Result(name = "success", type="redirectAction",location="blackAuditlistview")})
		public String blackAuditPass() 
		{
			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			try {
			
			  blackService.blackAuditPassService(black.getBlackNum(),op);
			  blackView();
				addActionMessage("黑名单审核成功~");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				addActionError("黑名单审核失败~");
			}

			return SUCCESS;
		}
	/**
	 * 审核不通过
	 * @return
	 */
	 @Action(value = "blackAuditNoPass", results = { @Result(name = "success", type="redirectAction",location="blackAuditlistview")})
		public String blackAuditNoPass()
		{
		    Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			try {
				blackService.blackAuditNoPassService(black.getBlackNum(),op);
				 blackView();
				addActionError("黑名单审核成功~");
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("黑名单审核失败~");
			}
			return SUCCESS;
		}
	 
	 
	 
	 
	 @Override
		protected BaseSearchCondition getSearchConditionFromSubClass()
				throws Exception {

			return searchCondition;
		}

		@Override
		protected void setSearchConditionToSubClass(
				BaseSearchCondition searchCondition) throws Exception {

			this.setSearchConditionToSubClass(searchCondition);
		}
		
		
		@Override
		protected void setSearchToConditionSession(
				BaseSearchCondition searchCondition) {

			getSession().setAttribute(SEARCH_SESSION_BLACK_LIST_AUDIT, searchCondition);
		}

		@Override
		protected BaseSearchCondition getSearchConditionFromSession() {

			return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_BLACK_LIST_AUDIT);
		}
		
		
		@Override
		protected String getInitPageSize() {

			return Constance.PAGE_MAX_SIZE+"";
		}
		public BlackAuditSearchCondition getSearchCondition() {
			return searchCondition;
		}
		public void setSearchCondition(BlackAuditSearchCondition searchCondition) {
			this.searchCondition = searchCondition;
		}


		public BlackListBean getBlack() {
			return black;
		}


		public void setBlack(BlackListBean black) {
			this.black = black;
		}


		public String getBlackNum() {
			return blackNum;
		}


		public void setBlackNum(String blackNum) {
			this.blackNum = blackNum;
		}
		
		
		
}
